import { Module } from 'vuex'
import { RootState } from '@/store/types'
import { fetchCompanyStatistics, fetchIcafeStatistics } from '@/api/main/index'

export interface DashboardModuleState {
  companyStatistics: CompanyStatistics
  icafeStatistics: IcafeStatistics
}

export interface CompanyStatistics {
  icafe: number
  user: number
  staff: number
  department: number
}

export interface IcafeStatistics {
  neimenggu: EchartsSeriesData[]
  category: EchartsSeriesData[]
  hohhot: EchartsSeriesData[]
}

interface EchartsSeriesData {
  name: string
  value: any
}

const dashboardModule: Module<DashboardModuleState, RootState> = {
  namespaced: true,
  state: () => {
    return {
      companyStatistics: {
        icafe: 0,
        user: 0,
        staff: 0,
        department: 0
      },
      icafeStatistics: {
        neimenggu: [],
        category: [],
        hohhot: []
      }
    }
  },
  getters: {
    companyStatistics(store) {
      return store.companyStatistics
    },
    icafeNeimengguStatistics(store) {
      return store.icafeStatistics.neimenggu
    },
    icafeCategoryStatistics(store) {
      return store.icafeStatistics.category
    },
    icafeHohhotStatistics(store) {
      return store.icafeStatistics.hohhot
    }
  },
  mutations: {
    changeCompanyStatistics(state, payload) {
      state.companyStatistics = payload
    },
    changeIcafeStatistics(state, payload) {
      state.icafeStatistics = payload
    }
  },
  actions: {
    async init({ commit }) {
      // 获取企业相关统计信息
      const companyStatistics = (await fetchCompanyStatistics())?.data ?? {}
      commit('changeCompanyStatistics', companyStatistics)

      // 获取网吧相关统计信息
      const icafeStatistics = (await fetchIcafeStatistics())?.data ?? []
      commit('changeIcafeStatistics', icafeStatistics)
    }
  }
}

export default dashboardModule
